package com.egdigital.allzic.data.json;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.egdigital.allzic.data.object.Station;
import com.egdigital.allzic.utils.Utils;

public class JsonParserStation {

	private static final String	TAG						= JsonParserStation.class.getSimpleName();	// log
	private static final String	TAG_ID					= "id";
	private static final String	TAG_TITLE				= "title";
	private static final String	TAG_SUBTITLE			= "subtitle";
	private static final String	TAG_GENRE				= "genre";
	private static final String	TAG_HQ					= "hq";
	private static final String	TAG_CATEGORY			= "category";
	private static final String	TAG_THUMB				= "thumb";
	private static final String	TAG_THUMB_SMARTPHONE	= "thumb_smartphone";
	private static final String	TAG_BACKGROUND			= "background";
	private static final String	TAG_URL					= "url";
	private static final String	TAG_FLUXAAC				= "flux_aac";
	private static final String	TAG_FLUXMP3				= "flux_mp3";
	private static final String	TAG_PLAYLIST			= "playlist";
	private static final String	TAG_HITS				= "hits";
	private static final String	TAG_LISTCATEGORY		= "categories";
	//
	private RequestQueue		mRequestQueue;
	private JSONArray			mJsonArrayStation		= null;
	private JSONArray			mJsonArrayCategory		= null;
	private JsonCallBack		mJsonCallback;

	public JsonParserStation() {
		super();
		initAndStartRequestQueue();
	}

	public void initAndStartRequestQueue() {
		mRequestQueue = VolleyManager.getRequestQueue();
		mRequestQueue.start();
	}

	public void stopRequestQueue() {
		mRequestQueue.stop();
	}

	public void extractJsonObjectData(String url) {
		JsonArrayRequest request = new JsonArrayRequest(url, createMyReqSuccessListener(), createMyReqErrorListener());
		request.setTag(this);
		mRequestQueue.add(request);
		mJsonCallback.downloadStarted();
	}

	public void setJsonCallBack(JsonCallBack callback) {
		mJsonCallback = callback;
	}

	protected Response.Listener<JSONArray> createMyReqSuccessListener() {
		return new Response.Listener<JSONArray>() {

			public void onResponse(JSONArray jsonArray) {
				List<Station> list = new ArrayList<Station>();
				String currentDateString = "" + Utils.getCurrentDate();
				try {
					mJsonArrayStation = jsonArray;
					for (int i = 0; i < mJsonArrayStation.length(); i++) {
						JSONObject jsonStation = mJsonArrayStation.getJSONObject(i);
						// get categories
						mJsonArrayCategory = jsonStation.getJSONArray(TAG_LISTCATEGORY);
						List<Integer> listcategory = new ArrayList<Integer>();
						for (int j = 0; j < mJsonArrayCategory.length(); j++) {
							listcategory.add(Integer.valueOf(1));
						}
						//
						mJsonCallback.downloadPercent(((i + 1) * 100) / mJsonArrayStation.length());
						Station station = new Station(jsonStation.getLong(TAG_ID), jsonStation.getString(TAG_TITLE), jsonStation.getString(TAG_SUBTITLE), jsonStation.getString(TAG_GENRE), jsonStation.getInt(TAG_HQ), jsonStation.getString(TAG_CATEGORY), jsonStation.getString(TAG_THUMB), jsonStation.getString(TAG_THUMB_SMARTPHONE), jsonStation.getString(TAG_BACKGROUND), jsonStation.getString(TAG_URL), jsonStation.getString(TAG_FLUXAAC), jsonStation.getString(TAG_FLUXMP3), jsonStation.getString(TAG_PLAYLIST), jsonStation.getInt(TAG_HITS), listcategory, currentDateString);
						list.add(station);
						// Log.i(TAG, station.getmFlux_aac() + " \n " + station.getmFlux_mp3() + "\n - \n");
					}
				} catch (Exception ex) {
				}
				mJsonCallback.downloadCompleted(list);
			}
		};
	}

	protected Response.ErrorListener createMyReqErrorListener() {
		return new Response.ErrorListener() {

			public void onErrorResponse(VolleyError error) {
				mJsonCallback.downloadEndedWithError(error);
			}
		};
	}
}